#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/9/13 11:26
# @Author  : 王凯
# @File    : neimenggu.py
# @Project : spider-man
import re

from apps.tax_illegal.tax_illegal.items import NetTaxIllegalProItem
from apps.tax_illegal.tax_illegal.spiders import BaseTaxIllegalSpider


class NEIMENGGUIllegalSpider(BaseTaxIllegalSpider):
    name = "neimenggu_illegal"
    province: str = "内蒙古"
    url: str = "https://neimenggu.chinatax.gov.cn/nsfw/sscx/zdaj/"  # 取首页

    def start_requests(self):
        yield from self.etax_search(**{"etax_url": "https://etax.neimenggu.chinatax.gov.cn:8443"})
        url = "https://neimenggu.chinatax.gov.cn/was5/search"
        data = {
            "channelid": "214090",
            "sortfield": "-DOCRELTIME",
            "searchword": "",
            "title": "",
            "zdaj_nsrsbh": "",
            "zdaj_zcdz": "",
            "zdaj_zzjgdm": "",
            "zdaj_fddb_xm": "",
            "zdaj_fddb_sfzh": "",
            "zdaj_cwfzr_xm": "",
            "zdaj_cwfzr_sfzh": "",
            "page": "1",
        }
        yield self.FormRequest(url, formdata=data, callback=self.parse)

    def parse(self, response, **kwargs):
        yield from self.parse_list(response, **kwargs)
        url = "https://neimenggu.chinatax.gov.cn/was5/search"
        page_total_num = response.xpath(".").re_first(r"总页数:(\d+)")
        for i in range(1, int(page_total_num) + 1):
            data = {
                "channelid": "214090",
                "sortfield": "-DOCRELTIME",
                "searchword": "",
                "title": "",
                "zdaj_nsrsbh": "",
                "zdaj_zcdz": "",
                "zdaj_zzjgdm": "",
                "zdaj_fddb_xm": "",
                "zdaj_fddb_sfzh": "",
                "page": f"{i}",
            }
            yield self.FormRequest(url, formdata=data, callback=self.parse_list)

    def parse_list(self, response, **kwargs):
        info = response.xpath('//*[@id="jjuuu"]/table/tbody//tr/td[5]/a/@href')
        for i in info:
            url = i.get()
            yield self.Request(url, callback=self.parse_detail_info)

    def parse_detail_info(self, response, **kwargs):
        item = NetTaxIllegalProItem()
        item.province = self.province

        info = []
        for i in response.xpath("//table//tr"):
            res = [i.xpath("string(.)").get().strip() for i in i.xpath(".//td")]
            if res:
                if len(res) != 2:
                    continue
                info.append(res)
        info = dict(info)

        for k, v in info.items():
            if "纳税人名称" in k:
                item.company_name = v
            if "纳税人识别号" in k:
                v = list([i for i in set(v.split(" ")) if i])
                item.taxpayer_id = v[0] if v else None
            if "组织机构代码" in k:
                item.org_code = v
            if "注册地址" in k:
                item.address = v
            if "法定代表人或者负责人姓名、性别、及身份证号码" in k:
                v = re.sub(r"(<.*?>.*<.*?>|\s*)", "", v, flags=re.S)
                if any(v.split("，")):
                    item.legal_representative = v.strip("...")
            if "违法期间法定代表人或责任人姓名、性别、身份证号码" in k:
                v = re.sub(r"(<.*?>.*<.*?>|\s*)", "", v, flags=re.S)
                if any(v.split("，")):
                    item.illegal_legal_representative = v.strip("...")
            if "负有直接责任的财务人员姓名、性别及身份证号码" in k:
                v = re.sub(r"(<.*?>.*<.*?>|\s*)", "", v, flags=re.S)
                if any(v.split("，")):
                    item.resp_financial = v.strip("...")
            if "实际负责人姓名、性别及身份证号码" in k:
                v = re.sub(r"(<.*?>.*<.*?>|\s*)", "", v, flags=re.S)
                if any(v.split("，")):
                    item.resp_person = v.strip("...")
            if "负有直接责任的中介机构信息" in k:
                v = re.sub(r"(<.*?>.*<.*?>|\s*)", "", v, flags=re.S)
                if any(v.split("，")):
                    item.resp_intermediary = v.strip("...")
            if "案件性质" in k:
                v = re.findall(r"var anj =\'(.*?)\';", v, flags=re.S)
                if v:
                    item.illegal_status = v[0]
            if "主要违法事实" in k:
                illegal_facts_src = v
                illegal_facts = re.findall(
                    r"(.*(?:主要存在|存在以下问题|发现其在)(?:[^根]*?[。，;；])*)[^。]*?(?:根据|依照|依据)+",
                    illegal_facts_src,
                )
                illegal_facts = illegal_facts[0] if illegal_facts else ""
                if illegal_facts:
                    basis_and_punishment = illegal_facts_src.replace(illegal_facts, "")
                else:
                    illegal_facts = illegal_facts_src
                    basis_and_punishment = ""
                item.basis_and_punishment = basis_and_punishment
                item.illegal_facts = illegal_facts

        pub_date = response.xpath('//*[@name="PubDate"]/@content').get()
        item.year = pub_date[:10].replace("-", "")
        yield item


if __name__ == "__main__":

    from scrapy import cmdline

    cmdline.execute(argv=["scrapy", "crawl", "neimenggu_illegal"])
